﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ArkanoidX.Sounds
{
    public class SoundResources
    {
        public SoundEffect seHit;
        public SoundEffect seKnock;
        public SoundEffect seShatter;
        public SoundEffect seTheme;
        public SoundEffect seLevel;
        public SoundEffect seMorph;
        public SoundEffect seShoot;

        public SoundEffectInstance seiTheme;
        public SoundEffectInstance seiShoot;
        public SoundEffectInstance seiLevel;

        TimeSpan lastHitPlay = default(TimeSpan);
        TimeSpan lastKnockPlay = default(TimeSpan);
        TimeSpan lastShootPlay = default(TimeSpan);

        MainGame _game;

        public SoundResources(MainGame game)
        {
            seHit = game.Content.Load<SoundEffect>("hit");
            seKnock = game.Content.Load<SoundEffect>("knock");
            seShatter = game.Content.Load<SoundEffect>("broken");
            seTheme = game.Content.Load<SoundEffect>("theme");
            seLevel = game.Content.Load<SoundEffect>("ready");
            seMorph = game.Content.Load<SoundEffect>("lengthen");
            seShoot = game.Content.Load<SoundEffect>("shoot");

            seiTheme = seTheme.CreateInstance();
            seiShoot = seShoot.CreateInstance();
            seiLevel = seLevel.CreateInstance();

            _game = game;
        }

        public void PlayHit()
        {
            if (lastHitPlay != default(TimeSpan) && 
                (_game.currentTime.TotalGameTime - lastHitPlay) < seHit.Duration)
            {
            }
            else
            {
                seHit.Play();
                lastHitPlay = _game.currentTime.TotalGameTime;
            }
        }

        public void PlayKnock()
        {
            if (lastKnockPlay != default(TimeSpan) &&
                (_game.currentTime.TotalGameTime - lastKnockPlay) < seKnock.Duration)
            {
            }
            else
            {
                seKnock.Play();
                lastKnockPlay = _game.currentTime.TotalGameTime;
            }
        }

        public void PlayShatter()
        {
            seShatter.Play();
        }

        public void PlayTheme()
        {
            if (seiLevel.State == SoundState.Playing)
                seiLevel.Stop();
            seiTheme.Play();
        }

        public void PlayMorph()
        {
            seMorph.Play();
        }

        public void PlayShoot()
        {
            if (lastShootPlay != default(TimeSpan) &&
                (_game.currentTime.TotalGameTime - lastShootPlay) < seShoot.Duration)
            {
            }
            else
            {
                seiShoot.Play();
                lastShootPlay = _game.currentTime.TotalGameTime;
            }
        }

        public void PlayLevelIntro()
        {
            if (seiTheme.State == SoundState.Playing)
            {
                seiTheme.Stop();
            }
            seiLevel.Play();
        }
    }
}
